
package com.example.project5;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends AppCompatActivity {

    private String mUsername; // 私有成员变量保存用户名
    private Button btnLogout;
    private Button btnMyProfile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // 2. 接收用户名并保存
        mUsername = getIntent().getStringExtra("USERNAME_KEY");

        // 初始化视图
        btnLogout = findViewById(R.id.btn_logout);
        btnMyProfile = findViewById(R.id.btn_my_profile);

        // 3. 设置按钮点击事件
        setupListeners();
    }

    private void setupListeners() {
        // "我的" 按钮点击事件
        btnMyProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, ProfileEditActivity.class);
                intent.putExtra("USERNAME_KEY", mUsername);
                startActivity(intent);
            }
        });

        // "退出登录" 按钮点击事件
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showLogoutDialog();
            }
        });
    }

    private void showLogoutDialog() {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确认是否退出登录？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击确定，返回登录页
                        Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish(); // 销毁主页
                    }
                })
                .setNegativeButton("取消", null) // 点击取消，对话框自动消失
                .show();
    }
}